﻿import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
//import com.yourcompany.ethernettest 1.0

import QtQuick.Window 2.15

import "." // 导入项目的根目录

Window {
    id: gpioPage
    visible: true
    width: 1200
    height: 768
    title: "gpio Test"


    // 这个函数是模拟获取gpio状态的函数，实际应用中需要替换为真实的获取状态逻辑
    function getgpioStatus() {
        // 模拟gpio状态数据，这里只是随机生成一个字符串作为示例
        var status = "数据更新" + Math.floor(Math.random() * 100);
        return status;
    }
    // 定时器，模拟gpio数据更新
    Timer {
        id: gpioTimer
        interval: 1000 // 更新间隔，单位为毫秒
        repeat: true   // 是否重复执行
        onTriggered: {
            // 这里是模拟gpio数据更新的地方
            // 假设我们有一个函数getgpioStatus()来获取gpio的状态
            var gpioStatus = getgpioStatus();

            // 更新文本显示框的内容
            gpioDataDisplay.text = "gpio状态: " + gpioStatus;
        }
    }

    Item {
        id: gpioItem
        anchors.fill : parent
        ColumnLayout {
            id: gpioColumn
            anchors.left : gpioItem.left
            height : gpioItem.height
            width : gpioItem.width * 0.90

            // 上半部分区域
            Rectangle {
                id:gpioTop
                width: gpioColumn.width * 0.99
                height: gpioColumn.height * 0.60
                anchors.top: gpioColumn.top
                anchors.topMargin: 3
                anchors.left: parent.left
                anchors.leftMargin: 3
                border.color: "#c0c0c0"     // 边框颜色
                border.width: 2             // 边框宽度

                color: "#f0f0f0"

                // 上半部分第一个框
                Rectangle {
                    id: gpioSetup
                    width: gpioTop.width * 0.99
                    height: gpioTop.height * 0.49
                    anchors.top: gpioTop.top
                    anchors.topMargin: 6
                    anchors.left: gpioTop.left
                    anchors.leftMargin: 6

                    ColumnLayout {
                        anchors.centerIn: parent
                        spacing: 10

                        RowLayout {
                            id: gpioTestGroup0
                            //anchors.fill: parent
                            width: gpioTop.width * 0.99
                            height: gpioTop.height * 0.10
                            anchors.top: gpioTop.top
                            anchors.topMargin: 5
                            anchors.left: gpioTop.left
                            anchors.leftMargin: 5
                            spacing: 15

                            Text {
                                id: gpioTestGroup0Text
                                width: gpioTop.width * 0.2
                                anchors.left: gpioTop.left
                                anchors.leftMargin: 5
                                text: "GPIO端口0:"
                            }

                            TextField {
                                id: gpioTestGroup0Input
                                width: gpioTop.width * 0.20
                                height: gpioSetup.height * 0.15
                                placeholderText: "gpio test"
                            }

                            Text {
                                id: gpioTestGroup0Interface
                                width: gpioTop.width * 0.2
                                anchors.left: gpioTop.left
                                anchors.leftMargin: 5
                                text: "测试地址:"
                            }

                            TextField {
                                id: gpioTestGroup0InterfaceInput
                                width: gpioTop.width * 0.20
                                height: gpioSetup.height * 0.15
                                placeholderText: "/data/ZhangDong/DAS1/APP/cpp/PrintTime/build/PrintTime"
                            }

                            Button {
                                id: clearDatabutton0up
                                text: "拉 高"
                                width: gpioTop.width * 0.20
                                height: gpioSetup.height * 0.15
                                onClicked: {
                                    var host = backend.host;
                                    var user = backend.user;
                                    var command ="/data/ZhangDong/DAS1/APP/cpp/PrintTime/build/PrintTime";
                                    gpiotest.sendRemoteCommand(host, user, gpioTestGroup0InterfaceInput.text);
                                    console.log("gpio 测试 接口 0");
                                }
                            }

                            Button {
                                id: clearDatabutton0down
                                text: "拉 低"
                                width: gpioTop.width * 0.20
                                height: gpioSetup.height * 0.15
                                onClicked: {
                                    console.log("gpio 测试 接口 0")
                                }
                            }
                        }

                        RowLayout {
                            id: gpioTestGroup1
                            width: gpioTop.width * 1
                            height: gpioTop.height * 0.1
                            anchors.top: gpioTestGroup0.bottom
                            anchors.topMargin: 5
                            anchors.left: gpioTop.left
                            anchors.leftMargin: 5
                            spacing: 15

                            Text {
                                id: gpioTestGroup1Text
                                width: gpioTop.width * 0.2
                                anchors.left: gpioTop.left
                                anchors.leftMargin: 5
                                text: "GPIO端口1:"
                            }

                            TextField {
                                id: gpioTestGroup1Input
                                width: gpioTop.width * 0.25
                                placeholderText: "gpio test"
                            }

                            Text {
                                id: gpioTestGroup1Interface
                                width: gpioTop.width * 0.2
                                anchors.left: gpioTop.left
                                anchors.leftMargin: 5
                                text: "测试地址:"
                            }

                            TextField {
                                id: gpioTestGroup1InterfaceInput
                                width: gpioTop.width * 0.20
                                height: gpioSetup.height * 0.15
                                placeholderText: "/data/ZhangDong/DAS1/APP/cpp/PrintTime/build/PrintTime"
                            }


                            Button {
                                id: clearDatabutton1up
                                text: "拉 高"
                                width: gpioTop.width * 0.20
                                height: gpioSetup.height * 0.15
                                onClicked: {
                                    var host = backend.host;
                                    var user = backend.user;
                                    var command ="/data/ZhangDong/DAS1/APP/cpp/PrintTime/build/PrintTime";
                                    gpiotest.sendRemoteCommand(host, user, gpioTestGroup1InterfaceInput.text);
                                    console.log("gpio 测试 接口 1");
                                }
                            }

                            Button {
                                id: clearDatabutton1down
                                text: "拉 低"
                                width: gpioTop.width * 0.20
                                height: gpioSetup.height * 0.15
                                onClicked: {
                                    console.log("gpio 测试 接口 0")
                                }
                            }
                        }

                        RowLayout {
                            id: gpioTestGroup2
                            width: gpioTop.width * 1
                            height: gpioTop.height * 0.1
                            anchors.top: gpioTestGroup1.bottom
                            anchors.topMargin: 5
                            anchors.left: gpioTop.left
                            anchors.leftMargin: 5
                            spacing: 15

                            Text {
                                id: gpioTestGroup2Text
                                width: gpioTop.width * 0.2
                                anchors.left: gpioTop.left
                                anchors.leftMargin: 5
                                text: "GPIO端口2:"
                            }

                            TextField {
                                id: gpioTestGroup2Input
                                width: gpioTop.width * 0.25
                                placeholderText: "gpio test"
                            }

                            Text {
                                id: gpioTestGroup2Interface
                                width: gpioTop.width * 0.2
                                anchors.left: gpioTop.left
                                anchors.leftMargin: 5
                                text: "测试地址:"
                            }

                            TextField {
                                id: gpioTestGroup2InterfaceInput
                                width: gpioTop.width * 0.20
                                height: gpioSetup.height * 0.15
                                placeholderText: "/data/ZhangDong/DAS1/APP/cpp/PrintTime/build/PrintTime"
                            }


                            Button {
                                id: clearDatabutton2up
                                text: "拉 高"
                                width: gpioTop.width * 0.20
                                height: gpioSetup.height * 0.15
                                onClicked: {
                                    var host = backend.host;
                                    var user = backend.user;
                                    var command ="/data/ZhangDong/DAS1/APP/cpp/PrintTime/build/PrintTime";
                                    gpiotest.sendRemoteCommand(host, user, gpioTestGroup2InterfaceInput.text);
                                    console.log("gpio 测试 接口 2");
                                }
                            }

                            Button {
                                id: clearDatabutton2down
                                text: "拉 低"
                                width: gpioTop.width * 0.20
                                height: gpioSetup.height * 0.15
                                onClicked: {
                                    console.log("gpio 测试 接口 0")
                                }
                            }
                        }

                        RowLayout {
                            id: gpioTestGroup3
                            width: gpioTop.width * 1
                            height: gpioTop.height * 0.1
                            anchors.top: gpioTestGroup2.bottom
                            anchors.topMargin: 5
                            anchors.left: gpioTop.left
                            anchors.leftMargin: 5
                            spacing: 15

                            Text {
                                id: gpioTestGroup3Text
                                width: gpioTop.width * 0.2
                                anchors.left: gpioTop.left
                                anchors.leftMargin: 5
                                text: "GPIO端口3:"
                            }

                            TextField {
                                id: gpioTestGroup3Input
                                width: gpioTop.width * 0.25
                                placeholderText: "gpio test"
                            }

                            Text {
                                id: gpioTestGroup3Interface
                                width: gpioTop.width * 0.2
                                anchors.left: gpioTop.left
                                anchors.leftMargin: 5
                                text: "测试地址:"
                            }

                            TextField {
                                id: gpioTestGroup3InterfaceInput
                                width: gpioTop.width * 0.20
                                height: gpioSetup.height * 0.15
                                placeholderText: "/data/ZhangDong/DAS1/APP/cpp/PrintTime/build/PrintTime"
                            }

                            Button {
                                id: clearDatabutton3up
                                text: "拉 高"
                                width: gpioTop.width * 0.20
                                height: gpioSetup.height * 0.15
                                onClicked: {
                                    var host = backend.host;
                                    var user = backend.user;
                                    var command ="/data/ZhangDong/DAS1/APP/cpp/PrintTime/build/PrintTime";
                                    gpiotest.sendRemoteCommand(host, user, gpioTestGroup3InterfaceInput.text);
                                    console.log("gpio 测试 接口 3");
                                }
                            }

                            Button {
                                id: clearDatabutton3down
                                text: "拉 低"
                                width: gpioTop.width * 0.20
                                height: gpioSetup.height * 0.15
                                onClicked: {
                                    console.log("gpio 测试 接口 0")
                                }
                            }
                        }
                    }
                }

                // 上半部分第二个框
                Rectangle {
                    id: gpioTopStatus
                    width: gpioTop.width * 0.99
                    height: gpioTop.height * 0.48
                    anchors.top: gpioSetup.bottom
                    anchors.topMargin: 4
                    anchors.left: gpioTop.left
                    anchors.leftMargin: 6

                    ColumnLayout {
                        anchors.centerIn: parent
                        spacing: 10

                        RowLayout {
                            id: gpioTestGroup4
                            //anchors.fill: parent
                            width: gpioTop.width * 0.99
                            height: gpioTop.height * 0.10
                            anchors.top: gpioTopStatus.top
                            anchors.topMargin: 5
                            anchors.left: gpioTop.left
                            anchors.leftMargin: 5
                            spacing: 15

                            Text {
                                id: gpioTestGroup4Text
                                width: gpioTop.width * 0.2
                                anchors.left: gpioTop.left
                                anchors.leftMargin: 5
                                text: "GPIO端口4:"
                            }

                            TextField {
                                id: gpioTestGroup4Input
                                width: gpioTop.width * 0.25
                                height: gpioSetup.height * 0.15
                                placeholderText: "gpio test"
                            }

                            Text {
                                id: gpioTestGroup4Interface
                                width: gpioTop.width * 0.2
                                anchors.left: gpioTop.left
                                anchors.leftMargin: 5
                                text: "测试地址:"
                            }

                            TextField {
                                id: gpioTestGroup4InterfaceInput
                                width: gpioTop.width * 0.20
                                height: gpioSetup.height * 0.15
                                placeholderText: "/data/ZhangDong/DAS1/APP/cpp/PrintTime/build/PrintTime"
                            }


                            Button {
                                id: clearDatabutton4up
                                text: "拉 高"
                                width: gpioTop.width * 0.20
                                height: gpioSetup.height * 0.15
                                onClicked: {
                                    var host = backend.host;
                                    var user = backend.user;
                                    var command ="/data/ZhangDong/DAS1/APP/cpp/PrintTime/build/PrintTime";
                                    gpiotest.sendRemoteCommand(host, user, gpioTestGroup4InterfaceInput.text);
                                    console.log("gpio 测试 接口 4")
                                }
                            }

                            Button {
                                id: clearDatabutton4down
                                text: "拉 低"
                                width: gpioTop.width * 0.20
                                height: gpioSetup.height * 0.15
                                onClicked: {
                                    console.log("gpio 测试 接口 0")
                                }
                            }
                        }

                        RowLayout {
                            id: gpioTestGroup5
                            //anchors.fill: parent
                            width: gpioTop.width * 0.99
                            height: gpioTop.height * 0.10
                            anchors.top: gpioTopStatus.top
                            anchors.topMargin: 5
                            anchors.left: gpioTop.left
                            anchors.leftMargin: 5
                            spacing: 15

                            Text {
                                id: gpioTestGroup5Text
                                width: gpioTop.width * 0.2
                                anchors.left: gpioTop.left
                                anchors.leftMargin: 5
                                text: "GPIO端口5:"
                            }

                            TextField {
                                id: gpioTestGroup5Input
                                width: gpioTop.width * 0.25
                                height: gpioSetup.height * 0.15
                                placeholderText: "gpio test"
                            }

                            Text {
                                id: gpioTestGroup5Interface
                                width: gpioTop.width * 0.2
                                anchors.left: gpioTop.left
                                anchors.leftMargin: 5
                                text: "测试地址:"
                            }

                            TextField {
                                id: gpioTestGroup5InterfaceInput
                                width: gpioTop.width * 0.20
                                height: gpioSetup.height * 0.15
                                placeholderText: "/data/ZhangDong/DAS1/APP/cpp/PrintTime/build/PrintTime"
                            }


                            Button {
                                id: clearDatabutton5up
                                text: "拉 高"
                                width: gpioTop.width * 0.20
                                height: gpioSetup.height * 0.15
                                onClicked: {
                                    var host = backend.host;
                                    var user = backend.user;
                                    var command ="/data/ZhangDong/DAS1/APP/cpp/PrintTime/build/PrintTime";
                                    gpiotest.sendRemoteCommand(host, user, gpioTestGroup5InterfaceInput.text);
                                    console.log("gpio 测试 接口 5")
                                }
                            }

                            Button {
                                id: clearDatabutton5down
                                text: "拉 低"
                                width: gpioTop.width * 0.20
                                height: gpioSetup.height * 0.15
                                onClicked: {
                                    console.log("gpio 测试 接口 0")
                                }
                            }
                        }

                        RowLayout {
                            id: gpioTestGroup6
                            //anchors.fill: parent
                            width: gpioTop.width * 0.99
                            height: gpioTop.height * 0.10
                            anchors.top: gpioTopStatus.top
                            anchors.topMargin: 5
                            anchors.left: gpioTop.left
                            anchors.leftMargin: 5
                            spacing: 15

                            Text {
                                id: gpioTestGroup6Text
                                width: gpioTop.width * 0.2
                                anchors.left: gpioTop.left
                                anchors.leftMargin: 5
                                text: "GPIO端口6:"
                            }

                            TextField {
                                id: gpioTestGroup6Input
                                width: gpioTop.width * 0.25
                                height: gpioSetup.height * 0.15
                                placeholderText: "gpio test"
                            }

                            Text {
                                id: gpioTestGroup6Interface
                                width: gpioTop.width * 0.2
                                anchors.left: gpioTop.left
                                anchors.leftMargin: 5
                                text: "测试地址:"
                            }

                            TextField {
                                id: gpioTestGroup6InterfaceInput
                                width: gpioTop.width * 0.20
                                height: gpioSetup.height * 0.15
                                placeholderText: "/data/ZhangDong/DAS1/APP/cpp/PrintTime/build/PrintTime"
                            }

                            Button {
                                id: clearDatabutton6up
                                text: "拉 高"
                                width: gpioTop.width * 0.20
                                height: gpioSetup.height * 0.15
                                onClicked: {
                                    var host = backend.host;
                                    var user = backend.user;
                                    var command ="/data/ZhangDong/DAS1/APP/cpp/PrintTime/build/PrintTime";
                                    gpiotest.sendRemoteCommand(host, user, gpioTestGroup6InterfaceInput.text);
                                    console.log("gpio 测试 接口 6");
                                }
                            }

                            Button {
                                id: clearDatabutton6down
                                text: "拉 低"
                                width: gpioTop.width * 0.20
                                height: gpioSetup.height * 0.15
                                onClicked: {
                                    console.log("gpio 测试 接口 6")
                                }
                            }
                        }

                        RowLayout {
                            id: gpioTestGroup7
                            //anchors.fill: parent
                            width: gpioTop.width * 0.99
                            height: gpioTop.height * 0.10
                            anchors.top: gpioTopStatus.top
                            anchors.topMargin: 5
                            anchors.left: gpioTop.left
                            anchors.leftMargin: 5
                            spacing: 15

                            Text {
                                id: gpioTestGroup7Text
                                width: gpioTop.width * 0.2
                                anchors.left: gpioTop.left
                                anchors.leftMargin: 5
                                text: "GPIO端口7:"
                            }

                            TextField {
                                id: gpioTestGroup7Input
                                width: gpioTop.width * 0.25
                                height: gpioSetup.height * 0.15
                                placeholderText: "gpio test"
                            }

                            Text {
                                id: gpioTestGroup7Interface
                                width: gpioTop.width * 0.2
                                anchors.left: gpioTop.left
                                anchors.leftMargin: 5
                                text: "测试地址:"
                            }

                            TextField {
                                id: gpioTestGroup7InterfaceInput
                                width: gpioTop.width * 0.20
                                height: gpioSetup.height * 0.15
                                placeholderText: "/data/ZhangDong/DAS1/APP/cpp/PrintTime/build/PrintTime"
                            }

                            Button {
                                id: clearDatabutton7up
                                text: "拉 高"
                                width: gpioTop.width * 0.20
                                height: gpioSetup.height * 0.15
                                onClicked: {
                                   var host = backend.host;
                                   var user = backend.user;
                                   var command ="/data/ZhangDong/DAS1/APP/cpp/PrintTime/build/PrintTime";
                                   gpiotest.sendRemoteCommand(host, user, gpioTestGroup7InterfaceInput.text);
                                   console.log("gpio 测试 接口 7");
                                }
                            }

                            Button {
                                id: clearDatabuttondown
                                text: "拉低"
                                width: gpioTop.width * 0.20
                                height: gpioSetup.height * 0.15
                                onClicked: {
                                    console.log("gpio 测试 接口 0")
                                }
                            }
                        }
                    }
                }
            }

            // 错误信息显示
            Text {
                id: gpioLogText
                color: "blue"
                anchors.top: gpioTop.bottom
                anchors.topMargin: 2
                anchors.left: gpioTop.left
                anchors.leftMargin: 10
                text: "输出日志:"
            }

            // 下半部分 显示数据
            Rectangle {
                id: gpioStatusDisplay
                width : gpioColumn.width  * 0.99
                height: gpioColumn.height * 0.35
                anchors.left: gpioTop.left
                anchors.top: gpioLogText.bottom
                anchors.topMargin: 4
                color: "#f0f0f0"
                border.color: "#c0c0c0"     // 边框颜色
                border.width: 2             // 边框宽度

                ScrollView{
                    id: scrollView
                    anchors.fill: parent
                    clip: true

                    // 文本显示框
                    TextArea {
                        id: gpioDataDisplay
                        anchors.fill: parent // 让 TextArea 填充整个 ScrollView
                        readOnly: true       // 使其只读，不可编辑
                        placeholderText: "接收到的数据"
                        color: "black"       // 设置字体颜色为黑色
                        background: Rectangle {
                            color: "transparent" // 背景透明，显示父元素的颜色
                        }
                        wrapMode: TextArea.Wrap // 自动换行
                        font.pixelSize: 14      // 字体大小（可以根据需要调整）
                    }
                }
            }

            // 该信号连接和注册方式，更加简单好用
            Connections {
                target: gpiotest
                onProcessFinished: {
                    console.log("Process finished!")
                }
                onCommandOutput: {
                    // 获取当前时间，并格式化为日志记录的格式
                    var time = Qt.formatDateTime(new Date(), "yyyy-MM-dd HH:mm:ss") // 格式化为: "2024-12-18 11:22:30"

                    // 将时间和输出内容一起追加到文本框,append会自动让文本显示在最新位置
                    gpioDataDisplay.append("[" +time + "] " + output)
                }
            }

            // 错误信息显示
            Text {
                id: errorDisplay
                color: "red"
                anchors.top: gpioStatusDisplay.bottom
                anchors.topMargin: 2
                anchors.left: gpioTop.left
                anchors.leftMargin: 10
                text: "未连接......"
            }
        }

        Rectangle {
            id: gpioInfo
            anchors.right: gpioItem.right
            height       : gpioItem.height * 0.97
            width        : gpioItem.width * 0.09
            anchors.top: gpioItem.top
            anchors.topMargin: 3
            anchors.rightMargin: 10
            border.color: "#c0c0c0"     // 边框颜色
            border.width: 2             // 边框宽度

            Button {
                id: gpioExitButton
                text: "返 回"
                width: gpioInfo.width * 0.80
                height: gpioInfo.height * 0.05
                anchors.left: gpioInfo.left
                anchors.leftMargin: gpioInfo.width * 0.1
                anchors.top: gpioInfo.top
                anchors.topMargin: 5
                onClicked: {
                    console.log("返回主界面")
                    gpioPage.visible = false
                    mainPage.visible = true
                }
            }

            Button {
                id: gpioClearButton
                text: "清 除"
                width: gpioInfo.width * 0.80
                height: gpioInfo.height * 0.05
                anchors.left: gpioInfo.left
                anchors.leftMargin: gpioInfo.width * 0.1
                anchors.top: gpioInfo.top
                anchors.topMargin: gpioInfo.height * 0.65
                onClicked: {
                    console.log("清除spi文本框信息")
                    gpioDataDisplay.clear()
                }
            }
        }
    }
}
